package com.orhon.smartcampus.modules.systemctl.fetcher;


import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.systemctl.dao.MenusDao;
import com.orhon.smartcampus.modules.systemctl.dao.OrgDepartmentUserRelationDao;
import com.orhon.smartcampus.modules.systemctl.entity.*;
import com.orhon.smartcampus.modules.systemctl.gqlModel.*;
import com.orhon.smartcampus.modules.systemctl.mm.*;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.Util;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public class DepartmentUsersRecordsFetcher implements DataFetcher {


	@Autowired
	OrgDepartmentUserRelationDao orgDepartmentUserRelationDao;

    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {
        
        List<GQLDepartmentUsers> departmentUsersArrayList = new ArrayList<>();


		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		String hql = "from OrgDepartmentUserRelation where deleted_at = null ";

		if(env.containsArgument("condition")) {
			HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
			if(map.get("record_id")!=null) {
				hql += "and id " + Util.decide(map.get("record_id"));
			}
			if(map.get("user_id")!=null) {
				hql += "and user_id "+ Util.decide(map.get("user_id"));
			}
			if(map.get("duty_id")!=null) {
				hql += "and duty_id "+ Util.decide(map.get("duty_id"));
			}

			if(map.get("department_id")!=null) {
				hql += "and department_id " +  Util.decide(map.get("department_id"));
			}
			if(map.get("is_leaders")!=null) {
				hql += "and  is_leaders " + Util.decide(map.get("is_leaders"));
			}
			

		}
		List<OrgDepartmentUserRelation> list = orgDepartmentUserRelationDao.find(hql,null,page.getPage(),page.getLimit());


		for (OrgDepartmentUserRelation item : list) {
			GQLUser sqlUser = null;
			if (env.getSelectionSet().contains("data/user")){
				Users users = item.getUserUsersByUserId();
				sqlUser = UserMapper.MAPPER.mapUser(users);
			}
			GQLDuty gqlDuty = null;
			if (env.getSelectionSet().contains("data/duty")){
				OrgDuties orgDuties = item.getSystemctlOrgDutiesByDutyId();
				gqlDuty = DutyMapper.MAPPER.toDuties(orgDuties);
			}

			GQLDepartment sqlDepartment= null;
			if (env.getSelectionSet().contains("data/department")){
				OrgDepartments orgDepartments = item.getSystemctlOrgDepartmentsByDepartmentId();
				sqlDepartment = DepartmentMapper.MAPPER.toOrgDepartments(orgDepartments);
			}


			GQLDepartmentUsers departmentUsers = DepartmentUsersRecordMapper.MAPPER.toOrgDepartmentUsers(item);
			departmentUsers.setUser(sqlUser);
			departmentUsers.setDuty(gqlDuty);
			departmentUsers.setDepartment(sqlDepartment);
			departmentUsersArrayList.add(departmentUsers);
		}

		Long total = 0L;
		if (env.getSelectionSet().contains("total")){
			total = orgDepartmentUserRelationDao.getTotalCount(hql);
		}
		return Result.ok().put("data", departmentUsersArrayList).put("total", total);
    }



}
